// Copyright 2015 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <cstdlib>
#include "font_manager.h"
#include "fplbase/fpl_common.h"

namespace flatui {

// A table to convert ISO 639 + ISO ISO3166-1 Alpha 2 based language code
// (e.g. 'en_US') to ISO 15924 script code (e.g. 'Latn') and a layout
// direction.
//
// The table is sorted in an alphabetic order, can be used with binary-search.
//
// Original information is retrieved from :
// http://stackoverflow.com/questions/16447807/get-an-iso-15924-script-code-for-a-given-ietf-language-tag-or-iso-639-iso-3166
// https://en.wikipedia.org/wiki/ISO_15924
// and modified.
const ScriptInfo FontManager::script_table_[] = {
    {"aa", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ab", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"abq", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"ace", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ach", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ada", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ady", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"ae", "Avst", nullptr, kTextLayoutDirectionRTL},
    {"af", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"agq", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"aii", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"ain", "Kana", nullptr, kTextLayoutDirectionLTR},
    {"ak", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"akk", "Xsux", nullptr, kTextLayoutDirectionLTR},
    {"ale", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"alt", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"am", "Ethi", "und-ethi", kTextLayoutDirectionLTR},  // Amharic
    {"amo", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"an", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"anp", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"ar", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"ar-IR", "Syrc", nullptr, kTextLayoutDirectionRTL},
    {"arc", "Armi", nullptr, kTextLayoutDirectionRTL},
    {"arn", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"arp", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"arw", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"as", "Beng", "as", kTextLayoutDirectionLTR},
    {"asa", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ast", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"av", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"awa", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"ay", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"az", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"az-AZ", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"az-IR", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"ba", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"bal", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"bal-IR", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"bal-PK", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ban", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ban-ID", "Bali", nullptr, kTextLayoutDirectionLTR},
    {"bas", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"bax", "Bamu", nullptr, kTextLayoutDirectionLTR},
    {"bbc", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"bbc-ID", "Batk", nullptr, kTextLayoutDirectionLTR},
    {"be", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"bej", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"bem", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"bez", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"bfq", "Taml", nullptr, kTextLayoutDirectionLTR},
    {"bft", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"bfy", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"bg", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"bh", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"bhb", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"bho", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"bi", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"bik", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"bin", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"bjj", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"bku", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"bla", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"blt", "Tavt", nullptr, kTextLayoutDirectionLTR},
    {"bm", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"bn", "Beng", "bn", kTextLayoutDirectionLTR},
    {"bo", "Tibt", nullptr, kTextLayoutDirectionLTR},
    {"bqv", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"br", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"bra", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"brx", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"bs", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"btv", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"bua", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"buc", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"bug", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"bug-ID", "Bugi", nullptr, kTextLayoutDirectionLTR},
    {"bya", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"byn", "Ethi", "und-ethi", kTextLayoutDirectionLTR},  // Blin
    {"ca", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"cad", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"car", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"cay", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"cch", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ccp", "Beng", nullptr, kTextLayoutDirectionLTR},
    {"ce", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"ceb", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"cgg", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ch", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"chk", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"chm", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"chn", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"cho", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"chp", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"chr", "Cher", nullptr, kTextLayoutDirectionLTR},
    {"chy", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"cja", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"cjm", "Cham", nullptr, kTextLayoutDirectionLTR},
    {"cjs", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"ckb", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"ckt", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"co", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"cop", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"cpe", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"cr", "Cans", nullptr, kTextLayoutDirectionLTR},
    {"crh", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"crk", "Cans", nullptr, kTextLayoutDirectionLTR},
    {"cs", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"csb", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"cu", "Glag", nullptr, kTextLayoutDirectionLTR},
    {"cv", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"cy", "Latn", "cy", kTextLayoutDirectionLTR},
    {"da", "Latn", "da", kTextLayoutDirectionLTR},
    {"dak", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"dar", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"dav", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"de", "Latn", "de-1996", kTextLayoutDirectionLTR},
    {"de-1901", "Latn", "de-1901", kTextLayoutDirectionLTR},
    {"de-BR", "Runr", nullptr, kTextLayoutDirectionLTR},
    {"de-CH-1901", "Latn", "de-ch-1901", kTextLayoutDirectionLTR},
    {"de-KZ", "Latn", "de-1996", kTextLayoutDirectionLTR},
    {"de-LI-1901", "Latn", "de-ch-1901", kTextLayoutDirectionLTR},
    {"de-US", "Latn", "de-1996", kTextLayoutDirectionLTR},
    {"del", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"den", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"dgr", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"din", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"dje", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"dng", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"doi", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"dsb", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"dua", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"dv", "Thaa", nullptr, kTextLayoutDirectionRTL},
    {"dyo", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"dyu", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"dz", "Tibt", nullptr, kTextLayoutDirectionLTR},
    {"ebu", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ee", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"efi", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"egy", "Egyp", nullptr, kTextLayoutDirectionLTR},
    {"eka", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"eky", "Kali", nullptr, kTextLayoutDirectionLTR},
    {"el", "Grek", nullptr, kTextLayoutDirectionLTR},
    {"en", "Latn", "en-gb", kTextLayoutDirectionLTR},
    {"en-AS", "Latn", "en-us", kTextLayoutDirectionLTR},
    {"en-GB", "Latn", "en-gb", kTextLayoutDirectionLTR},
    {"en-GU", "Latn", "en-us", kTextLayoutDirectionLTR},
    {"en-MH", "Latn", "en-us", kTextLayoutDirectionLTR},
    {"en-MP", "Latn", "en-us", kTextLayoutDirectionLTR},
    {"en-PR", "Latn", "en-us", kTextLayoutDirectionLTR},
    {"en-UM", "Latn", "en-us", kTextLayoutDirectionLTR},
    {"en-US", "Latn", "en-us", kTextLayoutDirectionLTR},
    {"en-VI", "Latn", "en-us", kTextLayoutDirectionLTR},
    {"eo", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"es", "Latn", "es", kTextLayoutDirectionLTR},
    {"et", "Latn", "et", kTextLayoutDirectionLTR},
    {"ett", "Ital", nullptr, kTextLayoutDirectionLTR},
    {"eu", "Latn", "eu", kTextLayoutDirectionLTR},
    {"evn", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"ewo", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"fa", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"fan", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ff", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"fi", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"fil", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"fil-US", "Tglg", nullptr, kTextLayoutDirectionLTR},
    {"fiu", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"fj", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"fo", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"fon", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"fr", "Latn", "fr", kTextLayoutDirectionLTR},
    {"frr", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"frs", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"fur", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"fy", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ga", "Latn", "ga", kTextLayoutDirectionLTR},
    {"gaa", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"gag", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"gag-MD", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"gay", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"gba", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"gbm", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"gcr", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"gd", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"gez", "Ethi", "und-ethi", kTextLayoutDirectionLTR},  // Geʻez
    {"gil", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"gl", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"gld", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"gn", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"gon", "Telu", nullptr, kTextLayoutDirectionLTR},
    {"gor", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"got", "Goth", nullptr, kTextLayoutDirectionLTR},
    {"grb", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"grc", "Cprt", nullptr, kTextLayoutDirectionRTL},
    {"grt", "Beng", nullptr, kTextLayoutDirectionLTR},
    {"gsw", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"gu", "Gujr", "gu", kTextLayoutDirectionLTR},
    {"guz", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"gv", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"gwi", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ha", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"ha-GH", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ha-NE", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"hai", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"haw", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"he", "Hebr", nullptr, kTextLayoutDirectionRTL},
    {"hi", "Deva", "hi", kTextLayoutDirectionLTR},
    {"hil", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"hit", "Xsux", nullptr, kTextLayoutDirectionLTR},
    {"hmn", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"hne", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"hnn", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ho", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"hoc", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"hoj", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"hop", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"hr", "Latn", "hr", kTextLayoutDirectionLTR},
    {"hsb", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ht", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"hu", "Latn", "hu", kTextLayoutDirectionLTR},
    {"hup", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"hy", "Armn", "hy", kTextLayoutDirectionLTR},
    {"hz", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ia", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"iba", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ibb", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"id", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ig", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ii", "Yiii", nullptr, kTextLayoutDirectionLTR},
    {"ii-CN", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ik", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ilo", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"inh", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"is", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"it", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"iu", "Cans", nullptr, kTextLayoutDirectionLTR},
    {"iu-CA", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ja", "Jpan", nullptr, kTextLayoutDirectionLTR},
    {"jmc", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"jpr", "Hebr", nullptr, kTextLayoutDirectionRTL},
    {"jrb", "Hebr", nullptr, kTextLayoutDirectionRTL},
    {"jv", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"jv-ID", "Java", nullptr, kTextLayoutDirectionLTR},
    {"ka", "Geor", nullptr, kTextLayoutDirectionLTR},
    {"kaa", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"kab", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"kac", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"kaj", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"kam", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"kbd", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"kca", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"kcg", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"kde", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"kdt", "Thai", nullptr, kTextLayoutDirectionLTR},
    {"kea", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"kfo", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"kfr", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"kg", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"kha", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"kha-IN", "Beng", nullptr, kTextLayoutDirectionLTR},
    {"khb", "Talu", nullptr, kTextLayoutDirectionLTR},
    {"khq", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"kht", "Mymr", nullptr, kTextLayoutDirectionLTR},
    {"ki", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"kj", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"kjh", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"kk", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"kk-KZ", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"kk-TR", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"kl", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"kln", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"km", "Khmr", nullptr, kTextLayoutDirectionLTR},
    {"kmb", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"kn", "Knda", "kn", kTextLayoutDirectionLTR},
    {"ko", "Kore", nullptr, kTextLayoutDirectionLTR},
    {"koi", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"kok", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"kos", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"kpe", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"kpy", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"kr", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"krc", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"kri", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"krl", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"kru", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"ks", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"ksb", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ksf", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ksh", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ku", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ku-LB", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"kum", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"kut", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"kv", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"kw", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ky", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"ky-CN", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"ky-TR", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"kyu", "Kali", nullptr, kTextLayoutDirectionLTR},
    {"la", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"lad", "Hebr", nullptr, kTextLayoutDirectionRTL},
    {"lag", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"lah", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"lam", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"lb", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"lbe", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"lcp", "Thai", nullptr, kTextLayoutDirectionLTR},
    {"lep", "Lepc", nullptr, kTextLayoutDirectionLTR},
    {"lez", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"lg", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"li", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"lif", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"lis", "Lisu", nullptr, kTextLayoutDirectionLTR},
    {"lki", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"lmn", "Telu", nullptr, kTextLayoutDirectionLTR},
    {"ln", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"lo", "Laoo", nullptr, kTextLayoutDirectionLTR},
    {"lol", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"loz", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"lt", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"lu", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"lua", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"lui", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"lun", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"luo", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"lus", "Beng", nullptr, kTextLayoutDirectionLTR},
    {"lut", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"luy", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"lv", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"lwl", "Thai", nullptr, kTextLayoutDirectionLTR},
    {"mad", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"mag", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"mai", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"mak", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"mak-ID", "Bugi", nullptr, kTextLayoutDirectionLTR},
    {"man", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"man-GN", "Nkoo", nullptr, kTextLayoutDirectionRTL},
    {"mas", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"mdf", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"mdh", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"mdr", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"men", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"mer", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"mfe", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"mg", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"mgh", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"mh", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"mi", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"mic", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"min", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"mk", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"ml", "Mlym", "ml", kTextLayoutDirectionLTR},
    {"mn", "Cyrl", "mn-cyrl", kTextLayoutDirectionLTR},
    {"mn-CN", "Mong", nullptr, kTextLayoutDirectionTTB},
    {"mnc", "Mong", nullptr, kTextLayoutDirectionTTB},
    {"mni", "Beng", nullptr, kTextLayoutDirectionLTR},
    {"mni-IN", "Mtei", nullptr, kTextLayoutDirectionLTR},
    {"mns", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"mnw", "Mymr", nullptr, kTextLayoutDirectionLTR},
    {"moh", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"mos", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"mr", "Deva", "mr", kTextLayoutDirectionLTR},
    {"ms", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"ms-MY", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ms-SG", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"mt", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"mua", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"mus", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"mwl", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"mwr", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"my", "Mymr", nullptr, kTextLayoutDirectionLTR},
    {"myv", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"myz", "Mand", nullptr, kTextLayoutDirectionRTL},
    {"na", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"nap", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"naq", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"nb", "Latn", "nb", kTextLayoutDirectionLTR},
    {"nd", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"nds", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ne", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"new", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"ng", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"nia", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"niu", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"nl", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"nmg", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"nn", "Latn", "nn", kTextLayoutDirectionLTR},
    {"no",                   "Latn", "nb",
     kTextLayoutDirectionLTR},  // Norwegian is very probably Norwegian Bokmål.
    {"nod", "Lana", nullptr, kTextLayoutDirectionLTR},
    {"nog", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"nqo", "Nkoo", nullptr, kTextLayoutDirectionRTL},
    {"nr", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"nso", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"nus", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"nv", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ny", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"nym", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"nyn", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"nyo", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"nzi", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"oc", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"oj", "Cans", nullptr, kTextLayoutDirectionLTR},
    {"om", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"om-ET", "Ethi", "und-ethi", kTextLayoutDirectionLTR},
    {"or", "Orya", "or", kTextLayoutDirectionLTR},
    {"os", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"osa", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"osc", "Ital", nullptr, kTextLayoutDirectionLTR},
    {"otk", "Orkh", nullptr, kTextLayoutDirectionRTL},
    {"pa", "Guru", "pa", kTextLayoutDirectionLTR},
    {"pa-PK", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"pag", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"pal", "Phli", nullptr, kTextLayoutDirectionRTL},
    {"pam", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"pap", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"pau", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"peo", "Xpeo", nullptr, kTextLayoutDirectionLTR},
    {"phn", "Phnx", nullptr, kTextLayoutDirectionRTL},
    {"pi", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"pl", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"pon", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"pra", "Brah", nullptr, kTextLayoutDirectionLTR},
    {"prd", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"prg", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"prs", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"ps", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"pt", "Latn", "pt", kTextLayoutDirectionLTR},
    {"qu", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"raj", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"rap", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"rar", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"rcf", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"rej", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"rej-ID", "Rjng", nullptr, kTextLayoutDirectionLTR},
    {"rjs", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"rkt", "Beng", nullptr, kTextLayoutDirectionLTR},
    {"rm", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"rn", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ro", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ro-RS", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"rof", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"rom", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"ru", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"rup", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"rw", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"rwk", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"sa", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"sad", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"saf", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"sah", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"sam", "Hebr", nullptr, kTextLayoutDirectionRTL},
    {"saq", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"sas", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"sat", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"saz", "Saur", nullptr, kTextLayoutDirectionLTR},
    {"sbp", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"sc", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"scn", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"sco", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"sd", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"sd-ID", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"sdh", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"se", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"se-NO", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"see", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"seh", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"sel", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"ses", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"sg", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"sga", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"shi", "Tfng", nullptr, kTextLayoutDirectionLTR},
    {"shn", "Mymr", nullptr, kTextLayoutDirectionLTR},
    {"si", "Sinh", nullptr, kTextLayoutDirectionLTR},
    {"sid", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"sk", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"sl", "Latn", "sl", kTextLayoutDirectionLTR},
    {"sm", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"sma", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"smi", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"smj", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"smn", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"sms", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"sn", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"snk", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"so", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"son", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"sq", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"sr", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"srn", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"srr", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ss", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ssy", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"st", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"su", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"suk", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"sus", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"sus-GN", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"sv", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"sw", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"swb", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"swb-YT", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"swc", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"syl", "Beng", nullptr, kTextLayoutDirectionLTR},
    {"syl-BD", "Sylo", nullptr, kTextLayoutDirectionLTR},
    {"syr", "Syrc", nullptr, kTextLayoutDirectionRTL},
    {"ta", "Taml", "ta", kTextLayoutDirectionLTR},
    {"tab", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"tbw", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"tcy", "Knda", nullptr, kTextLayoutDirectionLTR},
    {"tdd", "Tale", nullptr, kTextLayoutDirectionLTR},
    {"te", "Telu", "te", kTextLayoutDirectionLTR},
    {"tem", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"teo", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ter", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"tet", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"tg", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"tg-PK", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"th", "Thai", nullptr, kTextLayoutDirectionLTR},
    {"ti", "Ethi", "und-ethi", kTextLayoutDirectionLTR},  // Tigrinya
    {"tig", "Ethi", "und-ethi", kTextLayoutDirectionLTR},
    {"tiv", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"tk", "Latn", "tk", kTextLayoutDirectionLTR},
    {"tkl", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"tli", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"tmh", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"tn", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"to", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"tog", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"tpi", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"tr", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"tr-ED", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"tr-MK", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"tru", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"trv", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ts", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"tsg", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"tsi", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"tt", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"tts", "Thai", nullptr, kTextLayoutDirectionLTR},
    {"tum", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"tut", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"tvl", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"twq", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ty", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"tyv", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"tzm", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ude", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"udm", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"udm-RU", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"ug", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"ug-KZ", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"ug-MN", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"uga", "Ugar", nullptr, kTextLayoutDirectionLTR},
    {"uk", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"uli", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"umb", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"unr", "Beng", nullptr, kTextLayoutDirectionLTR},
    {"unr-NP", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"unx", "Beng", nullptr, kTextLayoutDirectionLTR},
    {"ur", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"uz", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"uz-AF", "Arab", nullptr, kTextLayoutDirectionRTL},
    {"uz-CN", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"vai", "Vaii", nullptr, kTextLayoutDirectionLTR},
    {"ve", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"vi", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"vi-US", "Hani", nullptr, kTextLayoutDirectionLTR},
    {"vo", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"vot", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"vun", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"wa", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"wae", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"wak", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"wal", "Ethi", "und-ethi", kTextLayoutDirectionLTR},  // Wolaytta
    {"war", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"was", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"wo", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"xal", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"xcr", "Cari", nullptr, kTextLayoutDirectionLTR},
    {"xh", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"xog", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"xpr", "Prti", nullptr, kTextLayoutDirectionRTL},
    {"xsa", "Sarb", nullptr, kTextLayoutDirectionRTL},
    {"xsr", "Deva", nullptr, kTextLayoutDirectionLTR},
    {"xum", "Ital", nullptr, kTextLayoutDirectionLTR},
    {"yao", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"yap", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"yav", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"yi", "Hebr", nullptr, kTextLayoutDirectionRTL},
    {"yo", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"yrk", "Cyrl", nullptr, kTextLayoutDirectionLTR},
    {"yue", "Hans", nullptr, kTextLayoutDirectionLTR},
    {"za", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"za-CN", "Hans", nullptr, kTextLayoutDirectionLTR},
    {"zap", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"zen", "Tfng", nullptr, kTextLayoutDirectionLTR},
    {"zh", "Hant", nullptr, kTextLayoutDirectionLTR},
    {"zh-CN", "Hans", nullptr, kTextLayoutDirectionLTR},
    {"zh-HK", "Hans", nullptr, kTextLayoutDirectionLTR},
    {"zh-MN", "Hans", nullptr, kTextLayoutDirectionLTR},
    {"zh-MO", "Hans", nullptr, kTextLayoutDirectionLTR},
    {"zh-SG", "Hans", nullptr, kTextLayoutDirectionLTR},
    {"zu", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"zun", "Latn", nullptr, kTextLayoutDirectionLTR},
    {"zza", "Arab", nullptr, kTextLayoutDirectionRTL}};

const char *FontManager::language_table_[] = {"de", "en", "es", "fr",
                                              "ja", "ko", "ru", "zh"};

// The lambda version of bsearch is not supported in VS2010, so write
// this old-school.
inline int CompareKeys(const void *key, const void *elem) {
  return strcmp(static_cast<const char *>(key),
                *static_cast<const char *const *>(elem));
}

bool FontManager::IsLanguageSupported(const char *language) {
  if (language == nullptr) {
    return false;
  }
  // Perform binary search.
  auto p = bsearch(language, language_table_, FPL_ARRAYSIZE(language_table_),
                   sizeof(const char *), CompareKeys);

  return p != nullptr;
}

inline int CompareKeyAndInfo(const void *key, const void *elem) {
  auto info = static_cast<const ScriptInfo *>(elem);
  return strcmp(static_cast<const char *>(key), info->locale);
}

const ScriptInfo *FontManager::FindLocale(const char *locale) {
  if (locale == nullptr) {
    return nullptr;
  }

  // Perform binary search.
  return static_cast<const ScriptInfo *>(
      bsearch(locale, script_table_, FPL_ARRAYSIZE(script_table_),
              sizeof(ScriptInfo), CompareKeyAndInfo));
}

}  // namespace flatui